#include "densityset.h"

densitySet::densitySet(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    ui_Label_Init();
    ui_Btn_Init();
   connect_Init();
}
void densitySet::connect_Init()
{

    //确定
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        keyPad *board = new keyPad(this);
         board->show();
        connect(board,&keyPad::changename2,this,&densitySet::Recive_Gas_SelData);
        connect(board,&keyPad::destroyed,[=](){
            btnlist_ui.at(1)->setFocus();
        });

    });

    //返回
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });

}
void densitySet::ui_Label_Init()
{

    for(int i=0;i<6;i++)//创建界面显示的label
    {
         theLabel[i] = new QLabel(this);
         theLabel[i]->setStyleSheet("QLabel{\
                                outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                                border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                border-style: outset;\
                                /* 按钮文本设置 */\
                                qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                font-size: 20px;\
                                color: #123567;			                 /* 文本颜色 */\
                           }"
          );
    }
    theLabel[0]->setGeometry(100,150,120,40);//设定每隔放置一个按键
    theLabel[1]->setGeometry(250,150,150,40);//设定每隔放置一个按键
    theLabel[2]->setGeometry(450,150,100,40);//设定每隔放置一个按键


    theLabel[0]->setText((Gas_Selname[GasSet_ABC-1]));
    theLabel[1]->setText(tr("钢瓶气浓度："));
    if(Sysset->Gas[GasSet_ABC-1].gasconcentration !=0)
    {
        theLabel[2]->setText(QString::number(Sysset->Gas[GasSet_ABC-1].gasconcentration,'f',0));
      }
    else
    {
    theLabel[2]->setText("-----");
    }

    theLabel[3]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
    theLabel[4]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
    theLabel[5]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

   theLabel[3]->setGeometry(0,0,640,60);//设定每隔放置一个按键
   theLabel[4]->setGeometry(0,0,300,60);//设定每隔放置一个按键
   theLabel[5]->setGeometry(340,0,300,60);//设定每隔放置一个按键
   theLabel[3]->setText("钢瓶器浓度设置");
}

void densitySet::ui_Btn_Init()
{

    for(int i=0;i<2;i++)//创建主屏幕显示的按键
    {
         theBtn[i] = new QPushButton(this);
}
btnlist_ui.append(theBtn[0]);
btnlist_ui.append(theBtn[1]);
theBtn[0]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/切换.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/切换2.png); /* 背景图片 */"+"}");
theBtn[1]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");

btnlist_ui.at(0)->setGeometry(150,300,120,90 );
btnlist_ui.at(1)->setGeometry(360,300,120,90 );
btnlist_ui.at(0)->setText(tr("修改"));
btnlist_ui.at(1)->setText(tr("返回"));

//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}
}

void densitySet::Recive_Gas_SelData(QString s)
{
   theLabel[2]->setText(QString::number(s.toFloat()));
   if(s.toFloat()!= Sysset->Gas[GasSet_ABC-1].gasconcentration) //改变钢瓶气浓度
   {
       if(Sysctrl->Sys_Gas_Sel == GasSet_ABC)  //改的气体正在出气
       {
           Sysctrl->Sys_Gas_Sel = 0;
           Sysctrl->Sys_Span_Sel = 0;
           Sysctrl->Total_Flow=0;
           Sysctrl->Target_Consentration=0;
           Sysset->Sys_Gas_Sel = Sysset->Sys_Span_sel = 0;
           Sysctrl->Sys_Ozo_Sel=Sysset->Sys_Only_Ozo = 0;
           Sysset->IS_OZO_ENABLE = 0;
           Sysctrl->BD_Ozonator.Vot_Set = 0;
           Sysset->PHOT_ENABLE = 0;
           Sysctrl->BD_Photometer.Vot_Set =0;
           Sysctrl->BD_Interface.Fflag = 1;//阀门控制切换标志 0:出来的时候为0  1:进去的时候为1
       }
       for(int i=0;i<7;i++)
       {
           Sysset->Gas[GasSet_ABC-1].gasconcentration_span[i] = 0;
           Sysset->Gas[GasSet_ABC-1].totalflow_span[i] = 0;
       }
   }
   Sysset->Gas[GasSet_ABC-1].gasconcentration = s.toFloat();

}

bool densitySet::eventFilter(QObject *watched, QEvent *event)
{
    if(btnlist_ui.at(0) == watched||btnlist_ui.at(1) == watched)
{
    if(event->type() == QEvent::KeyPress)
    {
        QKeyEvent *ev = (QKeyEvent *)event;
        QPushButton *button = (QPushButton *)watched;

       {
           switch (ev->key())
           {
           case OK:
           {
               emit  button-> clicked();
             break;
           }
           case UP: //Up
             {

               focusPreviousChild();
               break;
             }
           case DOWN: //Down
             {
              focusNextChild();
               break;
             }
           case BACK:
           {
               this->parentWidget()->show();
               this->close();
               break;
           }
           case INIT:
           {
               init_Show *show = new init_Show(this);
               show->show();
               this->hide();
               break;
           }
            default:
               break;
           }
       }
    }
    }
    return QWidget::eventFilter(watched,event);//返回父类的事件过滤器

}
